package com.codingvip.array;

/**
 * 输入: [1,2,3,4,5,6,7] 和 k = 3
 * 输出: [5,6,7,1,2,3,4]
 * 解释:
 * 向右旋转 1 步: [7,1,2,3,4,5,6]
 * 向右旋转 2 步: [6,7,1,2,3,4,5]
 * 向右旋转 3 步: [5,6,7,1,2,3,4]
 *
 * @author hansongtao
 */
public class Rotate {

    public static void main(String[] args) {
        Rotate rotate = new Rotate();
//        int[] nums = new int[]{1, 2, 3, 4, 5, 6, 7};
        int[] nums = new int[]{1,3};

        rotate.rotate(nums, 3);
        System.out.println(nums);

    }

    public void rotate(int[] nums, int k) {
        if (nums == null) {
            return;
        }
        reverse(nums, 0, nums.length - 1);
        reverse(nums, 0, (k-1)%(nums.length));
        if ((k - 1) % (nums.length) == nums.length - 1) {

        }else{
            reverse(nums, k%(nums.length), nums.length - 1);
        }

    }

    private void reverse(int[] nums, int start, int end) {
        int s = start, e = end;
        int temp;
        while (s < e) {
            temp = nums[e];
            nums[e] = nums[s];
            nums[s] = temp;
            s++;
            e--;
        }
    }
}
